<?php

namespace App;


/**
 * 查询每日记录
 * @desc 按分面读取数据
 * return int count 总数
 * return array data 分页返回的相关数据
 */

function rok_token_record($token_id,$user_id)
{
    //  按分页取数据
    $record = \PhalApi\DI()->notorm->rok_date_record
        ->where('token_id',$token_id)
        ->where('date', date("Y-m-d",time())  )
        ->fetchOne();
    if ( $record ){
        //  如果有record查询是否要去远征
        \PhalApi\DI()->notorm->rok_date_record
            ->where('id',$record['id'])
            ->update(
                array(
                    'token_id' => $token_id,
                    'count' => $record['count']+1,
                    'expedition' => $record['expedition']+1,
                    'start_up' => $record['start_up']+1,
                    'date' => date("Y-m-d",time()),
                    'ip'=> $_SERVER["REMOTE_ADDR"],
                    'time' => date("Y-m-d H:i:s",time()),
                )
            );
        if ( $record['expedition'] > 0 ){
            return true;
        }

    }else{
        \PhalApi\DI()->notorm->rok_date_record
            ->insert(
                array(
                    'token_id' => $token_id,
                    'date' => date("Y-m-d",time()),
                    'count' => 1,
                    'user_id' => $user_id,
                    'start_up' => 1,
                    'ip'=> $_SERVER["REMOTE_ADDR"],
                    'time' => date("Y-m-d H:i:s",time()),
                    'addtime' => date("Y-m-d H:i:s",time()),
                )
            );
    }
    return  false;
}


/**
 * 分页读取
 * @desc 按分面读取数据
 * return int count 总数
 * return array data 分页返回的相关数据
 */

function getInfoByPageBywhere($data)
{
	$page = $data->page;
	$size = $data->size;
	$where = json_decode($data->where,true);
	$order = $data->order;

	$newwhere = array();
	foreach ($where as $key=>$value){
	    if ($value == '0' ){
            $newwhere[$key]=$value;
        }elseif( $value){
			$newwhere[$key]=$value;
		}
	}
	$table = $data->table;


    $orderlist = array(
        'rok_imei' => "imei DESC",
        'rok_account' => "qus DESC,imei_id ASC",
    );
    if ( ! $order ){
        $order = $orderlist[$table];
    }
    if( ! $order ){
        $order = "id DESC";
    }

	// 按分页取数据
	$data = \PhalApi\DI()->notorm->$table
		->where($newwhere)
		->order($order)
		->limit(($page - 1) * $size, $size);
	// 读出总数
	$count = \PhalApi\DI()->notorm->$table
		->where($newwhere)
		->count("id");
	$arr = array(
		'count' => $count,
		'data' => $data,
	);
	return $arr;
}

/**
 * 分页读取
 * @desc 按分面读取数据
 * return int count 总数
 * return array data 分页返回的相关数据
 */

function getInfoByPage($data)
{
    $page = $data->page;
    $size = $data->size;
    $table = $data->table;

    // 按分页取数据
    $data = \PhalApi\DI()->notorm->$table
        ->order("id DESC")
        ->limit(($page - 1) * $size, $size);
    // 读出总数
    $count = \PhalApi\DI()->notorm->$table
        ->count("id");
    $arr = array(
        'count' => $count,
        'data' => $data,
    );
    return $arr;
}

/**
 * 按id更新 or 插入
 * @desc 根据id进行更新
 * return int 更新状态
 * return data  插入的数据
 */
function updateById($data)
{
    $id = $data->id;
    $arr = $data->arr;
    $table = $data->table;

    // 对数据进行还原
    $arr = json_decode($arr,true);
    $arr['time'] = date("Y-m-d H:i:s",time());
    if ($id){
        // 对该id进行更新
//        return array($id,$arr);
        return \PhalApi\DI()->notorm->$table
            ->where("id", $id)
            ->update($arr);
//        return \PhalApi\DI()->notorm->rok_account
//            ->where('id',$id)
//            ->fetchOne();
    }
    // 创建一个创建时间
    $arr['addtime'] = date("Y-m-d H:i:s",time());
    // 因为没有id所以新进一个数据
    return \PhalApi\DI()->notorm->$table
        ->insert($arr);
}

/**
 * 按id更新 or 插入
 * @desc 根据id进行更新
 * return int 更新状态
 * return data  插入的数据
 */
function updateByIddata($data)
{
	$id = $data['id'];
	$arr = $data['arr'];
	$table = $data['table'];

	// 对数据进行还原
	$arr = json_decode($arr,true);
	$arr['time'] = date("Y-m-d H:i:s",time());
	if ($id){
		// 对该id进行更新
		return \PhalApi\DI()->notorm->$table
			->where("id", $id)
			->update($arr);
	}
	// 创建一个创建时间
	$arr['addtime'] = date("Y-m-d H:i:s",time());
	// 因为没有id所以新进一个数据
	return \PhalApi\DI()->notorm->$table
		->insert($arr);
}

/**
 * 按id删除
 * @desc 根据id进行删除
 * return data 删除状态
 */
function deleteById($data)
{
    $id = $data->id;
    $table = $data->table;
    //通用的按id删除数据
    return \PhalApi\DI()->notorm->$table
        ->where("id",$id)
        ->DELETE();
}

/**
 * 创建一条任务
 * @desc 创建一条需要执行的任务
 * @param $data
 */
function creatOneTask($data)
{
    $js_name = $data['js_name'];
    $js_info = \PhalApi\DI()->notorm->news_js->where('js_name',$js_name)->fetchOne();
    $data['js_url'] = $js_info['js_url'];
    return \PhalApi\DI()->notorm->news_all_record->insert($data);
}

/**
 * 按条件查询任务
 * @desc 按条件查询任务
 * @param $data
 */
function checkOneTask($where)
{
    return \PhalApi\DI()->notorm->news_all_record->where($where)->fetchOne();
}

/**
 * 读出未完成的任务
 * @desc 按条件查询任务
 * @param $data
 */
function checkTask($group)
{
    return \PhalApi\DI()->notorm->news_task
        ->where(
            array(
                'state' => 'todo',
                'date < ?' => date("Y-m-d H:i:s",time()),
                'task_group' => $group
            )
        )
        ->order('id')
        ->fetchAll();
}


/**
 * task+1
 * @desc 按条件查询任务
 * @param $data
 */
function TaskAddOne($id)
{
    $info = \PhalApi\DI()->notorm->news_task
        ->where(
            array(
                'id' => $id,
            )
        )
        ->order('id')
        ->fetchOne();

    $todo = $info['todo'] + 1;
    $done = $info['done'];

    $update = array();
    $update['todo'] = $todo;
    if ($todo  >= $info['total']){
        $update['state'] = 'done';
    }
    \PhalApi\DI()->notorm->news_task
        ->where(
            array(
                'id' =>$id,
            )
        )
        ->update(
            $update
        );
}


/**
 * task+1
 * @desc 按条件查询任务
 * @param $data
 */
function Checkimei($imei,$phone_name,$note,$ip)
{
    $info = \PhalApi\DI()->notorm->rok_imei
        ->where('imei' , $imei)
        ->fetchOne();

    if($info){
        \PhalApi\DI()->notorm->rok_imei
            ->where('id' , $info['id'])
            ->update(
                array(
                    "ip" => $ip,
                    'phone_name'=> $phone_name,
                    'note'=> $note,
                    'time'=> date("Y-m-d H:i:s",time())
                )
            );
        return $info;
    }

    return \PhalApi\DI()->notorm->rok_imei
        ->insert(
            array(
                'time'=> date("Y-m-d H:i:s",time()),
                'addtime'=> date("Y-m-d H:i:s",time()),
                'phone_name'=> $phone_name,
                'note'=> $note,
                'imei'=> $imei,
            )
        );

}




function adphone(){

    $qusR = \PhalApi\DI()->notorm->rok_ad_lun
        ->where('id',1)
        ->fetchOne();
    $qus = $qusR['qus'];

    $maxqu = \PhalApi\DI()->notorm->rok_account
        ->where(array(
            'imei_id' => 88,
            'switch' => 0
        ))
        ->order( 'qus DESC' )
        ->fetchOne();

    if ( $qus >= $maxqu['qus'] ){
        $qus = 0;
    }

    $where = array(
        'imei_id' => 88,
        'switch' => 0,
        'qus > ?' => $qus,
    );

    $tokeninfo = \PhalApi\DI()->notorm->rok_account
        ->where($where)
        ->order('qus ASC')
        ->fetchOne();

    if ( $tokeninfo ){
        \PhalApi\DI()->notorm
            ->rok_account
            ->where('id',$tokeninfo['id'])
            ->update(array(
                    'todo' => 'todo',
                    'state' => '运行中',
                    'time' => date("Y-m-d H:i:s", time())
                )
            );

        \PhalApi\DI()->notorm
            ->rok_ad_lun
            ->where('id',1)
            ->update(array(
                    'qus' => $tokeninfo['qus'],
                )
            );
        return $tokeninfo;
    }









}








